package wechat_bot

import (
	"fmt"
	"github.com/eatmoreapple/openwechat"
	"reflect"
	"strings"
	"zy_bot_demo/viper"
)

var Blue7Groups []*openwechat.Group

func StartWechat() {
	InitWorker()

	bot := openwechat.DefaultBot(openwechat.Desktop) // 桌面模式
	//bot := openwechat.DefaultBot() // 桌面模式

	self, _ := bot.GetCurrentUser()
	dispatcher := openwechat.NewMessageMatchDispatcher()

	dispatcher.OnGroup(func(ctx *openwechat.MessageContext) {
		msg := ctx.Message
		sender, _ := msg.Sender()
		if sender.IsSelf() {
			return
		}
		group := openwechat.Group{
			User: sender,
		}
		//if !IsInArray(GroupNickNames, group.NickName) {
		if !IsInArray(viper.Config.Wechat.GroupNickNames, group.NickName) {
			return
		}
		content := msg.Content

		selfName := self.DisplayName
		fmt.Println("selfName: " + selfName)
		if IsBlank(selfName) {
			selfName = self.NickName
			fmt.Println("selfName: " + selfName)
		}
		if !strings.HasPrefix(content, fmt.Sprintf("@%v", selfName)) {
			return
		}
		fmt.Println("有人艾特机器人了，内容是：" + content)

		senderInGroup, _ := msg.SenderInGroup()
		name := senderInGroup.DisplayName
		if IsBlank(name) {
			name = senderInGroup.NickName
		}

		group.SendText(fmt.Sprintf("@%v \n %v", name, "请稍等片刻。。。。。"))

		message := content[len(selfName)+1:]
		task := GroupTask{
			GroupNickName:         group.NickName,
			Message:               message,
			SenderInGroupNickName: name,
		}
		worker.PutGroupTask(task)
		// 发送消息

		//fmt.Println("target.UserName: ", group.UserName)
		//fmt.Println("target.NickName: ", group.NickName)
		//fmt.Println("Text: ", msg.Content)
		//senderInGroup, _ := msg.SenderInGroup()
		//fmt.Println("group.UserName: ", senderInGroup.UserName)
		//fmt.Println("group.NickName: ", senderInGroup.NickName)
		//fmt.Println("Text: ", msg.Content)
	})
	// 注册消息回调函数
	bot.MessageHandler = dispatcher.AsMessageHandler()

	// 注册消息处理函数
	//bot.MessageHandler = func(msg *openwechat.Message) {
	//	if msg.IsText() && msg.Content == "ping" {
	//		msg.ReplyText("pong")
	//	}
	//}
	// 注册登陆二维码回调
	bot.UUIDCallback = openwechat.PrintlnQrcodeUrl

	// 创建 热登录，避免每次重启都需要扫码
	reloadStorage := openwechat.NewFileHotReloadStorage("storage.json")
	defer reloadStorage.Close()
	// 登陆
	if err := bot.PushLogin(reloadStorage, openwechat.NewRetryLoginOption()); err != nil {
		fmt.Println(err)
		panic(err)
	}

	// 获取登陆的用户
	self, err := bot.GetCurrentUser()
	if err != nil {
		fmt.Println(err)
		panic(err)
	}

	//// 获取所有的好友
	//friends, err := self.Friends()
	//fmt.Println(friends, err)

	// 获取所有的群组
	groups, err := self.Groups()
	fmt.Println(groups, err)
	Blue7Groups = groups

	// 阻塞主goroutine, 直到发生异常或者用户主动退出
	bot.Block()
	//return bot
}

func IsFloat64(v interface{}) bool {
	return reflect.TypeOf(v).Kind() == reflect.Float64
}

// GroupNickNames 更改为配置文件了
//var GroupNickNames []string = []string{
//	"003",
//}

func IsInArray(arr []string, target string) bool {
	for _, val := range arr {
		if val == target {
			return true
		}
	}
	return false
}

func IsBlank(str string) bool {
	return len(str) == 0
}

func IsNotBlank(str string) bool {
	return len(str) > 0
}

func FindGroupByGroupNickName(nickName string) *openwechat.Group {
	for _, v := range Blue7Groups {
		if v.NickName == nickName {
			return v
		}
	}
	return nil
}

func StartWechat1() {
	//bot := openwechat.DefaultBot(openwechat.Desktop) // 桌面模式
	bot := openwechat.DefaultBot() // 桌面模式

	// 注册消息处理函数
	bot.MessageHandler = func(msg *openwechat.Message) {
		if msg.IsText() && msg.Content == "ping" {
			msg.ReplyText("pong")
		}
	}
	// 注册登陆二维码回调
	bot.UUIDCallback = openwechat.PrintlnQrcodeUrl

	// 登陆
	if err := bot.Login(); err != nil {
		fmt.Println(err)
		//return nil
	}

	// 获取登陆的用户
	self, err := bot.GetCurrentUser()
	if err != nil {
		fmt.Println(err)
		//return nil
	}

	// 获取所有的好友
	friends, err := self.Friends()
	fmt.Println(friends, err)

	// 获取所有的群组
	groups, err := self.Groups()
	fmt.Println(groups, err)

	// 阻塞主goroutine, 直到发生异常或者用户主动退出
	bot.Block()
	//return bot
}
